package com.thuanpq60023.easyenglish.helper;

import java.io.File;

import jxl.Sheet;
import jxl.Workbook;
import android.content.Context;
import android.util.Log;

import com.thuanpq60023.easyenglish.adapter.DatabaseAdapter;
import com.thuanpq60023.easyenglish.bus.UnitBus;
import com.thuanpq60023.easyenglish.bus.VocabularyBus;
import com.thuanpq60023.easyenglish.entity.UnitEntity;
import com.thuanpq60023.easyenglish.entity.VocabEntity;

public class ReadXLS {
	private Context ctx;
	private VocabularyBus mVocabularyBus;
	private UnitBus mUnitBus;
	public static final String XSLNAME = "easyenglish.xls";
	private String strWord;
	private String strMean;
	private String strType;
	private String strExample;
	private String strUnit;
	private String strCreatedDate;
	private int intRight;
	private int intWrong;

	public ReadXLS(Context context) {
		this.ctx = context;
		mVocabularyBus = new VocabularyBus(ctx);
		mUnitBus = new UnitBus(ctx);
	}

	public String[] getSheetName() {
		String[] sheetsname = null;
		File inputWorkbook = new File(DatabaseAdapter.DATABASE_FILE_PATH
				+ File.separator + XSLNAME);
		Workbook w;
		try {
			if (inputWorkbook.exists()) {
				Log.e("ThuanPQ", "easyenglish.xls exists");
				w = Workbook.getWorkbook(inputWorkbook);
				sheetsname = w.getSheetNames();
			}
		} catch (Exception e) {
			Log.e("ThuanPQ", "getSheet(): " + e.toString());
		}
		return sheetsname;
	}

	public Sheet[] getSheet() {
		Sheet[] sheets = null;
		File inputWorkbook = new File(DatabaseAdapter.DATABASE_FILE_PATH
				+ File.separator + XSLNAME);
		Workbook w;
		try {
			if (inputWorkbook.exists()) {
				Log.e("ThuanPQ", "easyenglish.xls exists");
				w = Workbook.getWorkbook(inputWorkbook);
				sheets = w.getSheets();
			}
		} catch (Exception e) {
			Log.e("ThuanPQ", "getSheet(): " + e.toString());
		}
		return sheets;
	}

	public boolean multiImport(boolean[] sheetNo) {
		boolean flag = true;
		File inputWorkbook = new File(DatabaseAdapter.DATABASE_FILE_PATH
				+ File.separator + XSLNAME);
		Workbook w;
		try {
			if (inputWorkbook.exists()) {
				Log.e("ThuanPQ", "easyenglish.xls exists");

				w = Workbook.getWorkbook(inputWorkbook);
				for (int i = 0; i < sheetNo.length; i++) {
					if (sheetNo[i]) {

						Sheet sheet = w.getSheet(i);

						for (int j = 1; j < sheet.getRows(); j++) {
							strWord = sheet.getCell(0, j).getContents().trim();
							strMean = sheet.getCell(1, j).getContents().trim();
							strType = sheet.getCell(2, j).getContents().trim();
							strExample = sheet.getCell(3, j).getContents()
									.trim();
							strUnit = sheet.getCell(4, j).getContents().trim();
							strCreatedDate = GlobalFunction.getCurrentDate();
							intRight = Integer.parseInt(sheet.getCell(5, j)
									.getContents().trim());
							intWrong = Integer.parseInt(sheet.getCell(6, j)
									.getContents().trim());

							if (!strWord.equalsIgnoreCase("")
									&& !strMean.equalsIgnoreCase("")
									&& !strType.equalsIgnoreCase("")) {
								try {
									mUnitBus.insertUnit(new UnitEntity(strUnit,
											strCreatedDate));
									mVocabularyBus
											.insertVocab(new VocabEntity(
													strWord, strMean, strType,
													strExample, strUnit,
													strCreatedDate, intRight,
													intWrong));
									Log.e("ThuanPQ", "Add: " + strWord);
								} catch (Exception e) {
									flag = false;
									Log.e("ThuanPQ", e.getMessage().toString());
								}
							}
						}
					}
				}
			}
		} catch (Exception e) {
			flag = false;
			Log.e("ThuanPQ", "run(): " + e.toString());
		}

		return flag;
	}
}